package org.lwd.datalink.api.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.lwd.datalink.domain.Position;
import org.lwd.datalink.zookeeper.DLinkZkPathDef;
import org.lwd.datalink.zookeeper.DLinkZkUtils;

/**
 * @Author: weidong.lin
 * @Description: 任务位点
 * @Date: create in 2024/3/18 17:04
 */
@Slf4j
public class TaskPositionBiz {


    /**
     * 更新指定task的位点信息
     *
     * @param taskId
     * @param position
     * @param positionEnum
     */
    public static void updatePosition(String taskId, Position position) {
        String path = DLinkZkPathDef.getTaskPositionNode(taskId);
        DLinkZkUtils.updateTask(taskId, JSON.toJSONBytes(position, SerializerFeature.WriteClassName), path);
    }

    /**
     * 获取指定Task的位点信息，如果没有则返回null
     *
     * @param taskId
     * @return
     */
    public static Position getPosition(String taskId) {
        String path = DLinkZkPathDef.getTaskPositionNode(taskId);
        byte[] data = DLinkZkUtils.getTask(taskId, path);

        if (data == null || data.length == 0) {
            return null;
        }
        return JSON.parseObject(data, Position.class);
    }
}
